package Merlion.userManagement.bean;

import Merlion.userManagement.entity.SystemUserEntity;
import Merlion.userManagement.entity.TaskEntity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless
public class TaskSessionBean implements TaskSessionBeanRemote  {

   @PersistenceContext
   EntityManager em;
   @EJB
   private CurrentSessionBeanRemote csb;
   
   
   public Long addTask(String title, String task, 
                       Date due, int hourD, int minuteD, int secondD,
                       boolean reminder, int reminderTime){
       
       Long returnID = Long.parseLong("0");
       
       TaskEntity newTask = new TaskEntity();
       
          Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            Date dateNow = currentDate.getTime();
       
       String currentId = csb.getCurrentId();
       SystemUserEntity user = em.find(SystemUserEntity.class, currentId);
       String ownerName = user.getName();
       if(user!=null){
           System.out.println("Title is "+title);
           System.out.println("Task is: "+task);
           
           newTask.create(currentId,ownerName, user, title, task,dateNow,  due,
                          hourD, minuteD, secondD, reminder);
           
           em.persist(newTask);
           newTask.setReminderTime(reminderTime);
           newTask.setDeleteFlag("N");
           em.flush();
           returnID = newTask.getId();
       }
       return returnID;
   
   }
   
   public void editTask(Long id, String title, String task, 
                       Date due, int hourD, int minuteD, int secondD,
                       boolean reminder, int reminderTime){
       TaskEntity t = em.find(TaskEntity.class, id);
       if(t!=null){
           t.setTitle(title);
           t.setTask(task);
           t.setDateDue(due);
           t.setHourDue(hourD);
           t.setMinuteDue(minuteD);
           t.setSecondDue(secondD);
           t.setReminder(reminder);
           t.setReminderTime(reminderTime);
           em.flush();
       }
   }
   
   public void deleteTask(Long id){
       TaskEntity t = em.find(TaskEntity.class, id);
       t.setDeleteFlag("Y");
   }
   
   public void updateTask(){
       
       Calendar currentDate = Calendar.getInstance();
       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
       Date dateNow = currentDate.getTime();
       String d1 = formatter.format(dateNow);
       
       Calendar calendar = new GregorianCalendar();
       int hour = calendar.get(Calendar.HOUR_OF_DAY);
       int minute = calendar.get(Calendar.MINUTE);
       int second = calendar.get(Calendar.SECOND);
       
       List<TaskEntity> taskList = viewTask();
       
       for(Object o : taskList){
           TaskEntity t = (TaskEntity) o;
           if (!t.getDeleteFlag().equals("Y")) {
                   Date date = t.getDateDue();
                   SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                   String d2 = sdf.format(date);

                   if (d1.equalsIgnoreCase(d2)) {
                       int reminderTime = t.getReminderTime();
                       int hourDue = t.getHourDue();
                       int minuteDue = t.getMinuteDue();
                       int secondDue = t.getSecondDue();
                       
                       if(hour>hourDue){
                           t.setExpired(true);
                           em.flush();
                       }else if(minute>minuteDue && hour<=hourDue){
                           t.setExpired(true);
                           em.flush();
                       }else if(second>secondDue && minute<=minuteDue && hour <= hourDue){
                           t.setExpired(true);
                           em.flush();
                       }
                   }
             }
       }
   }
   
   public void updateExpiryStatus(Long id){
       TaskEntity task = em.find(TaskEntity.class, id);
       task.setExpired(true);
       em.flush();
   }
   
   public List<TaskEntity> checkReminder(){
      
       System.out.println("Check Reminder");
       Calendar currentDate = Calendar.getInstance();
       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
       Date dateNow = currentDate.getTime();
       String d1 = formatter.format(dateNow);
       
       Calendar calendar = new GregorianCalendar();
       int hour = calendar.get(Calendar.HOUR_OF_DAY);
       int minute = calendar.get(Calendar.MINUTE);
       int second = calendar.get(Calendar.SECOND);
       
       List<TaskEntity> taskResult = new ArrayList<TaskEntity>();
       
       String currentId = csb.getCurrentId();
       SystemUserEntity user = em.find(SystemUserEntity.class, currentId);
       if(user!=null){
           Query q = em.createQuery("SELECT a from TaskEntity a where a.ownerId = :currentId").setParameter("currentId", currentId);
           List<TaskEntity> taskList = q.getResultList();
           for(Object o : taskList){
               
               TaskEntity t = (TaskEntity) o;
               if (!t.getDeleteFlag().equals("Y")) {
                   Date date = t.getDateDue();
                   SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                   String d2 = sdf.format(date);

                   if (d1.equalsIgnoreCase(d2)) {
                       int reminderTime = t.getReminderTime();
                       int hourDue = t.getHourDue();
                       int minuteDue = t.getMinuteDue();
                       int secondDue = t.getSecondDue();

                       if (Math.abs(minuteDue - minute) == reminderTime) {
                           System.out.println("Task found! ");
                           if (!t.isExpired()) {
                               taskResult.add(t);
                               t.setExpired(true);
                           }
                        }
                    
                
                    }
               
                }
            }
       }
       
       return taskResult;
   }
   
   public List<TaskEntity> viewTask(){
       
       String currentId = csb.getCurrentId();
       
       //updateTask();
       
       List<TaskEntity> taskResult = new ArrayList<TaskEntity>();
       
       Query q = em.createQuery("Select a from TaskEntity a where a.ownerId = :currentId").setParameter("currentId", currentId);
       for(Object o : q.getResultList()){
           TaskEntity t= (TaskEntity) o;
           if(!t.getDeleteFlag().equals("Y")){      
               taskResult.add(t);
            }
       }
       
  
       return taskResult;
   }
                 
   
    
}
